# -- coding: utf-8 --**


from flask import *
import json

from 点餐系统.工具类.untils import json_crud

app = Flask(__name__)
app.secret_key = 'nyh'

menu_json_url = '../点餐系统/数据类/menu.json'

member_json_url = '../点餐系统/数据类/member_list.json'

json_crud = json_crud()

menu_list = json_crud.get_json_kv(menu_json_url)

member_list1 = {'ddd': 'asd', 'adm': 'pwd'}

gl_username = ''


@app.route("/", methods=["GET", "POST"])
def web01():
    login_Infos = [
        {
            "name": "herculians",
            "pwd": "milano"
        }
    ]

    return render_template('alert.html', items=login_Infos)


@app.after_request
def cors(environ):
    environ.headers['Access-Control-Allow-Origin'] = '*'
    environ.headers['Access-Control-Allow-Method'] = '*'
    environ.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
    return environ


'''
目的：实现登录功能
1.get和post功能的实现 --》 需要判断请求方式
2.获取请求参数
3.验证参数
4.返回success
'''

'''
flash消息闪现
--> 设置secretkey 做消息的混淆
模板中需要遍历信息
'''


@app.route("/login/", methods=["GET", "POST"])
def login():
    global gl_username
    if request.method == "POST":
        username = request.form.get('username')
        password = request.form.get('password')
        password2 = request.form.get('password2')
        print(member_list1)
        print(type(member_list1))
        if not all([username, password, password2]):
            print("输入信息不完整！")
            flash(u"message not full")
        elif password != password2:
            print('两次密码不一致！')
            flash(u"check the password2")
        else:
            # 此处记得校验账号密码
            # 根据账号密码 查询键值对是否存在
            if password == member_list1.get(username, '未查询到账号！'):
                print('登陆成功，欢迎进入餐厅，{}!'.format(username))
                gl_username = username
                return redirect(url_for('menu', username=username))
            else:
                print('账户或者密码错误')
                flash(u"请检查账户或者密码！")
            # return redirect('/menu')
    return render_template('login.html')


@app.route("/menu/", methods=["GET", "POST"])
def menu():
    return render_template('menu.html', menu_list1=menu_list, username=gl_username)


@app.route("/add/", methods=["GET", "POST"])
def add():
    if request.method == "POST":
        username = request.form.get('new_username')
        password = request.form.get('new_password')
        password2 = request.form.get('new_password2')
        print("username:" + username)
        if not all([username, password, password2]):
            print("输入信息不完整！")
            flash(u"message not full")
        elif password != password2:
            print('两次密码不一致！')
            flash(u"check the password2")
        else:
            # 检查账号是否已经存在
            if username in member_list1.keys():
                flash(u"该账号已经注册！")
            else:
                member_list1[username] = password  # 将账户密码信息写入字典键值对
                print(member_list1)
                return redirect('/login')
    return render_template('add.html')


if __name__ == '__main__':
    app.run(port=81)
